﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using HtmlAgilityPack;

namespace BitworkStudios.Scraper.Document
{
    public class Structure<T>
    {
        public string DocumentName { get; set; }
        public List<Element> Elements { get; set; }
        private HtmlDocument _htmlDocument;

        public DataTable ElementsDataSource
        {
            get
            {
                DataTable elementsDataTable = Elements.ToDataTable();
                elementsDataTable.TableName = DocumentName;
                return elementsDataTable;
            }
        }

        public Structure()
        {
            Elements = Element.GetTemplate<T>();
        }

        public Structure(string elementDefinitionFilePath)
        {
            var csv = new CsvReader(new StreamReader(elementDefinitionFilePath));
            Elements = csv.GetRecords<Element>().ToList();
        }

        public void Scrape(string htmlDocument)
        {
            Elements.ClearElements();

            _htmlDocument = new HtmlDocument();
            _htmlDocument.LoadHtml(htmlDocument);

            foreach (Element element in Elements)
            {
                element.GetNode(_htmlDocument);
            }
        }
    }
}
